<template>
  <!-- 协催管理-诉讼案件-->
  <section class="page assist-case">
    <data-form :model="model" @handleResetForm="handleResetForm" @onSearch="refreshData">
      <template slot="inputs">
        <el-form-item label="订单编号" prop="caseNumber">
          <el-input v-model="model.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="model.personalName"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="model.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="model.idCard"></el-input>
        </el-form-item>
        <el-form-item label="欠款金额" prop="overdueAmount">
          <number-range :isInteger="true" v-model="model.overdueAmount"></number-range>
        </el-form-item>
        <el-form-item label="逾期期数" prop="overduePeriod">
          <el-select clearable placeholder="请选择" v-model="model.overduePeriod">
            <el-option :key="index" :label="name" :value="code.replace(/M|\+/, '')" v-for="{index,name,code} in $dict.getDictData('OverduePeriodStatus')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="逾期天数" prop="overdueDays">
          <number-range :isInteger="true" v-model="model.overdueDays"></number-range>
        </el-form-item>
        <el-form-item label="产品类型" prop="productCategory">
          <el-input v-model="model.productCategory"></el-input>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="model.productName"></el-input>
        </el-form-item>
        <el-form-item label="诉讼编号" prop="lawNumber">
          <el-input v-model="model.lawNumber"></el-input>
        </el-form-item>
        <el-form-item label="诉讼状态" prop="lawerStatus">
          <el-select clearable placeholder="请选择" v-model="model.lawerStatus">
            <el-option :key="index" :label="name" :value="code" v-for="{index,name,code} in $dict.getDictData('LawerStatus')"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="顾客类型" prop="customerCategory">
          <el-select v-model="model.customerCategory" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('CustomerCategory')" :key="index" :label="name"
                       :value="code"></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item label="创建时间" prop="creatTime">
          <el-date-picker :editable="false" clearable placeholder="选择日期时间" type="daterange" v-model="model.creatTime"></el-date-picker>
        </el-form-item>
        <el-form-item label="案件标签" prop="labelName">
          <get-label-name :formModel="model" v-model="model.labelName"></get-label-name>
        </el-form-item>
      </template>
    </data-form>

    <data-box :data="dataSet" :page="pageService" :selectionList.sync="selectionList" @onPageChange="refreshData">
      <template slot="buttons">
        <el-button @click="distributeClick" v-auth="1513">重新分案</el-button>
      </template>
      <template slot="columns">
        <el-table-column :min-width="$helper.getColumnWidth(6)" label="操作">
          <template slot-scope="scope">
            <el-button @click="isView=false,lawsuitClick(scope.row)" type="text" v-auth="1516" v-if="scope.row.assistStatus !== 'ASSIST_OVER'">诉讼跟进</el-button>
            <el-button @click="isView=true,lawsuitClick(scope.row)" type="text" v-auth="1517" v-if="scope.row.assistStatus === 'ASSIST_OVER' && scope.row.lawerStatus && scope.row.lawsuitFollowType">跟进查看</el-button>
            <el-button @click="rowData=scope.row, dialog.isOpenCostMaintenance=true" type="text" v-auth="1518">费用维护</el-button>
          </template>
        </el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(6)" label="订单编号" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button @click="openExecutionPage(scope.row)" type="text">{{scope.row.caseNumber}}</el-button>
          </template>
        </el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(6)" label="诉讼编号" prop="lawNumber" show-overflow-tooltip></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="客户姓名" prop="personalName" show-overflow-tooltip></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="手机号" prop="mobileNo" show-overflow-tooltip></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(6)" label="身份证号" prop="idCard" show-overflow-tooltip></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="欠款金额(元)" prop="overdueAmount" show-overflow-tooltip></el-table-column>
        <el-table-column :formatter="row => $common.isNotValue(row.overduePeriod) ? '':`M${row.overduePeriod}`" :min-width="$helper.getColumnWidth(3)" label="逾期期数" prop="overduePeriod"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="逾期天数" prop="overdueDays" show-overflow-tooltip></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="产品类型" prop="productCategory" show-overflow-tooltip></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="产品名称" prop="productName" show-overflow-tooltip></el-table-column>
        <!-- <el-table-column prop="customerCategory" label="顾客类型"
                         :formatter="(row) => $filter.dictConvert(row.customerCategory,'CustomerCategory')"
                         :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>-->
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="诉讼跟进人" prop="assistCollectorName" show-overflow-tooltip></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(5)" label="电催/外访催收员" prop="currentCollectorName" show-overflow-tooltip></el-table-column>
        <el-table-column :formatter="(row) => $filter.dateTimeFormat(row.flowInTime)" :min-width="$helper.getColumnWidth(4)" label="案件创建时间" prop="flowInTime" show-overflow-tooltip></el-table-column>
        <el-table-column :formatter="(row) => $filter.dictConvert(row.lawerStatus,'LawerStatus')" :min-width="$helper.getColumnWidth(4)" label="诉讼状态" prop="lawerStatus" show-overflow-tooltip></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(10)" label="案件标签" prop="labelName"></el-table-column>
      </template>
    </data-box>

    <!-- 重新分配 -->
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" :visible.sync="dialog.ManualDivision" title="重新分案" width="800px" @open="$nextTick(()=>$refs['case-allot'].refreshDept())">
      <case-allot :fromChannel="caseChannel" @close="dialog.ManualDivision=false" @refreshList="refreshData" again ref="case-allot"></case-allot>
    </el-dialog>

    <!-- 诉讼跟进-未确定诉讼类型的情况 -->
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" :visible.sync="dialog.isOpenLawsuitKind" center title="诉讼跟进">
      <lawsuit-kind :lawsuit="lawsuit" :rowData="rowData" @close="dialog.isOpenLawsuitKind = false" @refreshData="refreshData" ref="lawsuit-kind"></lawsuit-kind>
    </el-dialog>

    <!-- 诉讼跟进-已经确定诉讼类型的情况 -->
    <el-dialog :visible.sync="dialog.isOpenLawsuitFollow" @close="refreshData" @open="() => this.$nextTick(() => this.$refs['lawsuit-follow'].refresh(rowData.lawerStatus))" append-to-body center title="诉讼跟进" width="900px">
      <lawsuit-follow :isView="isView" :lawsuit.sync="lawsuit" :rowData="rowData" @close="dialog.isOpenLawsuitFollow = false" @refreshData="refreshData" ref="lawsuit-follow"></lawsuit-follow>
    </el-dialog>

    <!-- 费用管理 -->
    <el-dialog :center="true" :visible.sync="dialog.isOpenCostMaintenance" @close="refreshData" @open="$nextTick(()=>$refs['cost-maintain'].refreshCostData())" title="费用管理" width="1200px">
      <cost-maintain :rowData="rowData" @close="dialog.isOpenCostMaintenance = false" @refreshData="refreshData" ref="cost-maintain"></cost-maintain>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Dependencies, Layout, Auth } from "~/core/decorator";
import { PageService } from "~/utils/page.service";
import { SortService } from "~/utils/sort.service";

import CaseAllot from "~/components/distribution-common/case-allot.vue";
import NumberRange from "~/components/common/number-range.vue";
import { CaseAssitCaseService } from "~/services/domain-service/case-assist-case.service.ts";
import CostMaintain from "~/components/outsource-manage/outsource-lawsuit-manage/cost-maintain.vue";
import LawsuitKind from "~/components/lawsuit-follow/lawsuit-kind.vue";
import LawsuitFollow from "~/components/lawsuit-follow/lawsuit-follow.vue";
import { OutsourceLawsuitService } from "~/services/domain-service/outsource-lawsuit-manage-index.service";
import { CASE_CHANNEL, MENU_CHANNEL } from "~/config/enum.config";
import { Getter, namespace } from "vuex-class";
import GetLabelName from "~/components/labeling-strategy/get-label-name.vue";

const AllotModule = namespace("case-allot");

@Auth(1504)
@Layout("workspace")
@Component({
  components: {
    GetLabelName,
    CaseAllot,
    NumberRange,
    LawsuitKind,
    CostMaintain,
    LawsuitFollow
  }
})
export default class AssistCase extends Vue {
  @Dependencies(PageService)
  private pageService: PageService;
  @Dependencies(SortService)
  private sortService: SortService;
  @Dependencies(CaseAssitCaseService)
  private caseAssitCaseService: CaseAssitCaseService;
  @Dependencies(OutsourceLawsuitService)
  private outsourceLawsuitService: OutsourceLawsuitService;
  @AllotModule.Mutation private updateCaseIds;
  @AllotModule.Mutation private updateExtData;
  @Getter
  private lawDepartmentDataNoRoot;
  @Getter
  private lawDepartmentData;
  private caseChannel = CASE_CHANNEL.law;

  private rowData: any = {};

  private get lawsuit() {
    return {
      kind: this.rowData.lawsuitFollowType,
      status: this.rowData.lawerStatus
    };
  }

  // 跟进是否只能查看
  private isView: boolean = false;

  private model: any = {
    areaIds: [],
    areaId: "",
    addressDetail: "",
    overdueDays: {
      max: "",
      min: ""
    },
    overdueAmount: {
      max: "",
      min: ""
    },
    turnSource: "",
    productCategory: "",
    creatTime: [],
    productName: "",
    lawNumber: "",
    customerCategory: "",
    overduePeriod: "",
    lawerStatus: "",
    labelName: []
  };
  private dataSet: any[] = [];
  private selectionList: any[] = [];
  private dialog: any = {
    ManualDivision: false,
    isOpenLawsuitKind: false,
    isOpenLawsuitFollow: false,
    isOpenCostMaintenance: false
  };

  private mounted() {
    this.refreshData();
  }

  private refreshData() {
    this.caseAssitCaseService
      .queryCaseAssistlist(this.model, this.pageService, this.sortService)
      .subscribe(data => {
        this.dataSet = data;
        this.selectionList = [];
      });
  }

  /**
   * 手动分案
   */
  private distributeClick() {
    if (this.selectionList.length === 0) {
      this.$message.info("请选择要分配的案件");
      return;
    }

    this.updateCaseIds(this.selectionList.map(v => v.caseId));
    const caseAssistIds = this.selectionList.map(v => v.id);
    this.updateExtData({ caseAssistIds });
    this.dialog.ManualDivision = true;
  }

  /**
   * 获取地区id
   */
  private getAreaId() {
    this.model.areaId =
      this.model.areaIds.length > 0
        ? this.model.areaIds[this.model.areaIds.length - 1]
        : "";
  }

  /**
   * 点击诉讼跟进
   */
  private lawsuitClick(rowData) {
    this.rowData = rowData;
    // 如果存在诉讼类型 跳过选择诉讼类型的一步
    if (!rowData.lawsuitFollowType) {
      this.dialog.isOpenLawsuitKind = true;
    } else {
      this.dialog.isOpenLawsuitFollow = true;
    }
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.$router.openCaseDetail(row.caseId, {
      menuChannel: MENU_CHANNEL.Lawsuit,
      hiddenCaseList: true,
      hiddenAction: true
    });
  }

  /**
   * 范围输入框的重置
   */
  private handleResetForm() {
    this.model.overdueAmount = { min: "", max: "" };
    this.model.overdueDays = { max: "", min: "" };
  }
}
</script>

<style lang="less">
</style>
